﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Sukul.PhotoTools.BL
{
    public sealed class ThreadManager
    {
        delegate void AsyncMethodCaller();
        /* A store of all created threads. */
        ArrayList _threads = new ArrayList();
        private int _activeFileCount = 0;

        public delegate void ProgressEventHandler(object sender, ProgressEventArgs e);
        public event ProgressEventHandler progress;

        private ThreadManager()
        {}

        private static ThreadManager _instance = null;
        private static object lockO = new object();
        public static ThreadManager Instance
        {
            get
            {
                if (null == _instance)
                {
                    lock (lockO)
                    {
                        if (null == _instance)
                        {
                            _instance = new ThreadManager();
                        }
                    }
                }
                return _instance;
            }
        }

        public string StartFolder
        {
            get;
            set;
        }

        public string EndFolder
        {
            get;
            set;
        }

        public void Start()
        {
            string destFilename = string.Empty;
            string destPath = string.Empty;
            DateTime fileDateCreated;

            foreach (var file in Directory.GetFiles(StartFolder))
            {
                fileDateCreated = File.GetCreationTime(file);
                destPath = string.Format(@"{0}\{1}\{2}\{3}",
                                EndFolder,
                                fileDateCreated.Year,
                                fileDateCreated.Month,
                                fileDateCreated.Day);
                destFilename = string.Format(@"{0}\{1}", destPath, Path.GetFileName(file));

                if (!Directory.Exists(destPath))
                {
                    Directory.CreateDirectory(destPath);
                }

                File.Copy(file, destFilename);
            }
        }

        public void ThreadFinishedEvent()
        {
            if (null != progress)
            {
                progress(this, new ProgressEventArgs { FileCount=_activeFileCount });
            }
        }

    }


    public class ProgressEventArgs : EventArgs
    {
        public int FileCount
        {
            get;
            set;
        }
    }
}
